# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2Json_v2 import Table2Json

from com.zzh.utils.pagingUtils import MyPaging
from com.zzh.utils.sqlUtils.SqlUtils import SqlUtils
from com.zzh.utils.requestFieldMapDBField import fieldMap_Chen

import time

localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

class IndusItemDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()

    def insertIndusItem(self, info):
        """
        添加 industry_item 信息
        :param info:
        :return:
        """
        industryId = info['industryId']
        itemId = info['itemId']

        sql = "insert into zzh_industry_item (industry_id, item_id) VALUES " \
              "(%s, %s)"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [industryId, itemId])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def findIndusItemById(self, indusId):
        """
        根据ID查询 industry_item 信息
        :param indusId:
        :return:
        """
        sql = "select * from zzh_industry_item WHERE id=%s"

        cur = self.conn.cursor()
        rs = cur.execute(sql, [indusId])
        jsons = None
        if rs:
            list0 = cur.fetchall()
            jsons = Table2Json.indus_item_list_to_json(list0)
        cur.close()
        self.conn.close()
        return jsons

    def findIndusItemByCondition(self, **kwargs):
        """
        industry_item 表的条件查询
        :param kwargs:
        :return:
        """

        conditions = kwargs['conditions']
        multiSort = kwargs['multiSort']
        pageIndex = kwargs['pageIndex']
        pageSize = kwargs['pageSize']



        fieldMap = fieldMap_Chen.industryItemMap#要改的这个
        whereSentence = SqlUtils.getOption(conditions, fieldMap)

        cur = self.conn.cursor()
        if multiSort:
            # 排序拼接
            sortRule = SqlUtils.getSortRule(multiSort, fieldMap)
            sql = "SELECT * FROM zzh_industry_item WHERE " + whereSentence + " ORDER BY " + sortRule
        else:
            sql = "SELECT * FROM zzh_industry_item WHERE " + whereSentence
        rs = cur.execute(sql)

        total_count = cur.execute(sql)
        if total_count:
            result = cur.fetchall()

            cur.close()
            self.conn.close()
            resultList= MyPaging().getPage(total_count,pageIndex,pageSize,result)
            userOauthJSON = Table2Json.indus_item_list_to_json(resultList)
            return userOauthJSON,total_count
        else:

            cur.close()
            self.conn.close()
            return None,0
        """
        itemId = kwargs['itemId']

        sql = "select * from zzh_industry_item WHERE item_id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [itemId])
        jsons = None
        if rs:
            list0 = cur.fetchall()
            jsons = Table2Json.indus_item_list_to_json(list0)
        cur.close()
        self.conn.close()
        return jsons

        """

    def deleteIndus(self, idArray):
        """
        根据ID删除信息，可以传递一个或者多个ID，ID以数组形式传递
        :param idArray:
        :return:
        """
        sql = "delete from zzh_industry_item WHERE id=%s"
        cur = self.conn.cursor()
        for idd in idArray:
            rs = cur.execute(sql, [idd])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def updateIndus(self, info):
        """
        更新 industry_item 表
        :param info:
        :return:
        """
        mId = info['id']
        industryId = info['industryId']
        itemId = info['itemId']

        sql = "update zzh_industry_item set industry_id=%s, item_id=%s WHERE id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [industryId, itemId, mId])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def selectIndusItem(self, id):
        """
        根据ID查询，返回元组
        :param id:
        :return: data tuple
        """
        sql = "select * from zzh_industry_item WHERE id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [id])
        arr = cur.fetchone()
        cur.close()
        self.conn.close()
        return arr